package Arkkomeet

import org.springframework.dao.DataIntegrityViolationException

class CatalogoParticipanteController {
	def CatalogoParticipanteService

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index() {
		redirect(action: "list", params: params)
	}

	def list(Integer max) {
		String nombre=params.catPartNombre
		String apellido = params.catPartApellido1

		def oParticipante = CatalogoParticipanteService.busquedaFiltrada(params)
		if(oParticipante.size==0){
			params.max = Math.min(max ?: 10, 100)
			[catalogoParticipanteInstanceList: CatalogoParticipante.list(params), catalogoParticipanteInstanceTotal: CatalogoParticipante.count()]
		}
		else{

			[catalogoParticipanteInstanceList: oParticipante, catalogoParticipanteInstanceTotal: CatalogoParticipante.count()]

		}
	}

	def create() {
		[catalogoParticipanteInstance: new CatalogoParticipante(params)]
	}

	def save() {
		flash.valNombreBlank= ""
		flash.valApellido1Blank=""
		flash.valCorreoElectronicoBlank=""
		flash.valCorreoElectronicoUnique=""
		def catalogoParticipanteInstance = new CatalogoParticipante(params)
		if (!catalogoParticipanteInstance.save(flush: true)) {
			if(params.catPartNombre.equals("")){
				flash.valNombreBlank = message(code: 'CatalogoParticipante.catPartNombre.blank')
			}
			if(params.catPartApellido1.equals("")){
				flash.valApellido1Blank = message(code: 'CatalogoParticipante.catPartApellido1.blank')
			}
			if(params.catPartCorreoElectronico.equals("")){
				flash.valCorreoElectronicoBlank = message(code: 'CatalogoParticipante.catPartCorreoElectronico.blank')
			}
			
//			if(message(code: 'CatalogoParticipante.catPartCorreoElectronico.Unique')){
//				flash.valCorreoElectronicoUnique = message(code: 'CatalogoParticipante.catPartCorreoElectronico.unique')
//			}
					
			render(view: "create", model: [catalogoParticipanteInstance: catalogoParticipanteInstance])
			return
		}
		String nombre = CatalogoParticipanteService.formatoNombre(params)
		catalogoParticipanteInstance.setCatPartNombre(nombre)
		String apellido1  = CatalogoParticipanteService.formatoApellido1(params)
		catalogoParticipanteInstance.setCatPartApellido1(apellido1)
		String correoElectronico  = CatalogoParticipanteService.formatoCorreoElectronico(params)
		catalogoParticipanteInstance.setCatPartCorreoElectronico(correoElectronico)
		String CodigoUsuario  = CatalogoParticipanteService.formatoCodigoUsuario(params)
		catalogoParticipanteInstance.setCatPartCodigoUsuario(CodigoUsuario)
		
		if(!params.catPartApellido2.equals("")){
			String apellido2  = CatalogoParticipanteService.formatoApellido2(params)
			catalogoParticipanteInstance.setCatPartApellido2(apellido2)
		}
		if(!params.catPartObservaciones.equals("")){	
			String observaciones  = CatalogoParticipanteService.formatoObservaciones(params)
			catalogoParticipanteInstance.setCatPartObservaciones(observaciones)
		}
		if(!params.catPartProcedencia.equals("")){
			String procedencia  = CatalogoParticipanteService.formatoProcedencia(params)
			catalogoParticipanteInstance.setCatPartProcedencia(procedencia)
		}
		flash.message = message(code: 'default.created.message', args: [message(code: 'catalogoParticipante.label', default: 'CatalogoParticipante'), catalogoParticipanteInstance.id])
		redirect(action: "show", id: catalogoParticipanteInstance.id)
	}

	def show(Long id) {
		def catalogoParticipanteInstance = CatalogoParticipante.get(id)
		if (!catalogoParticipanteInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'catalogoParticipante.label', default: 'CatalogoParticipante'), id])
			redirect(action: "list")
			return
		}
		if(catalogoParticipanteInstance.getCatPartEstado()== 1){
			flash.valEstado= "Activo"
		}
		else{
			flash.valEstado = "Inactivo"
		}
		if(catalogoParticipanteInstance.getCatPartTipo()== 1){
			flash.valTipo= "Interno"
		}
		else{
			flash.valTipo = "Externo"
		}
		if(catalogoParticipanteInstance.getCatPartCodigoRolParticipante()== 1){
			flash.valRol= "Super Usuario"
		}
		if(catalogoParticipanteInstance.getCatPartCodigoRolParticipante()== 2){
			flash.valRol= "Usuario Administrador"
		}
		if(catalogoParticipanteInstance.getCatPartCodigoRolParticipante()== 3){
			flash.valRol= "Usuario con Privilegios"
		}
		if(catalogoParticipanteInstance.getCatPartCodigoRolParticipante()== 4){
			flash.valRol= "Usuario Restringido"
		}
		if(catalogoParticipanteInstance.getCatPartCodigoRolParticipante()== 5){
			flash.valRol= "Usuario de Lecciones Aprendidas"
		}
		[catalogoParticipanteInstance: catalogoParticipanteInstance]
	}

	def edit(Long id) {
		def catalogoParticipanteInstance = CatalogoParticipante.get(id)
		if (!catalogoParticipanteInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'catalogoParticipante.label', default: 'CatalogoParticipante'), id])
			redirect(action: "list")
			return
		}
		if(catalogoParticipanteInstance.getCatPartEstado()==1){

		}

		[catalogoParticipanteInstance: catalogoParticipanteInstance]
		
		//				println catalogoParticipanteInstance.errors
		
	}

	def update(Long id, Long version) {
		flash.valNombreBlank= ""
		flash.valApellido1Blank=""
		flash.valCorreoElectronicoBlank=""
		flash.valCorreoElectronicoUnique=""
		def catalogoParticipanteInstance = CatalogoParticipante.get(id)
		if (!catalogoParticipanteInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'catalogoParticipante.label', default: 'CatalogoParticipante'), id])
			redirect(action: "list")
			return
		}

		if (version != null) {
			if (catalogoParticipanteInstance.version > version) {
				catalogoParticipanteInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[message(code: 'catalogoParticipante.label', default: 'CatalogoParticipante')] as Object[],
						"Another user has updated this CatalogoParticipante while you were editing")
				render(view: "edit", model: [catalogoParticipanteInstance: catalogoParticipanteInstance])
				return
			}
		}

		catalogoParticipanteInstance.properties = params

		if (!catalogoParticipanteInstance.save(flush: true)) {
			if(params.catPartNombre.equals("")){
				flash.valNombreBlank = message(code: 'CatalogoParticipante.catPartNombre.blank')
			}
			if(params.catPartApellido1.equals("")){
				flash.valApellido1Blank = message(code: 'CatalogoParticipante.catPartApellido1.blank')
			}
			if(params.catPartCorreoElectronico.equals("")){
				flash.valCorreoElectronicoBlank = message(code: 'CatalogoParticipante.catPartCorreoElectronico.blank')
			}
			else{
				if(message(code: 'CatalogoParticipante.catPartCorreoElectronico.Unique')){
					flash.valCorreoElectronicoUnique = message(code: 'CatalogoParticipante.catPartCorreoElectronico.unique')
				}
			}
			render(view: "edit", model: [catalogoParticipanteInstance: catalogoParticipanteInstance])
			return
		}
		String nombre = CatalogoParticipanteService.formatoNombre(params)
		catalogoParticipanteInstance.setCatPartNombre(nombre)
		String apellido1  = CatalogoParticipanteService.formatoApellido1(params)
		catalogoParticipanteInstance.setCatPartApellido1(apellido1)
		String correoElectronico  = CatalogoParticipanteService.formatoCorreoElectronico(params)
		catalogoParticipanteInstance.setCatPartCorreoElectronico(correoElectronico)
		String CodigoUsuario  = CatalogoParticipanteService.formatoCodigoUsuario(params)
		catalogoParticipanteInstance.setCatPartCodigoUsuario(CodigoUsuario)
		
		if(!params.catPartApellido2.equals("")){
			String apellido2  = CatalogoParticipanteService.formatoApellido2(params)
			catalogoParticipanteInstance.setCatPartApellido2(apellido2)
		}
		if(!params.catPartObservaciones.equals("")){	
			String observaciones  = CatalogoParticipanteService.formatoObservaciones(params)
			catalogoParticipanteInstance.setCatPartObservaciones(observaciones)
		}
		if(!params.catPartProcedencia.equals("")){
			String procedencia  = CatalogoParticipanteService.formatoProcedencia(params)
			catalogoParticipanteInstance.setCatPartProcedencia(procedencia)
		}
		if(catalogoParticipanteInstance.getCatPartTipo() == 2){
			catalogoParticipanteInstance.setCatPartCodigoUsuario("")
			catalogoParticipanteInstance.setCatPartCodigoRolParticipante(0)
		}
		flash.message = message(code: 'default.updated.message', args: [message(code: 'catalogoParticipante.label', default: 'CatalogoParticipante'), catalogoParticipanteInstance.id])
		redirect(action: "show", id: catalogoParticipanteInstance.id)
	}

	def delete(Long id) {
		def catalogoParticipanteInstance = CatalogoParticipante.get(id)
		if (!catalogoParticipanteInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'catalogoParticipante.label', default: 'CatalogoParticipante'), id])
			redirect(action: "list")
			return
		}

		try {
			catalogoParticipanteInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'catalogoParticipante.label', default: 'CatalogoParticipante'), id])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'catalogoParticipante.label', default: 'CatalogoParticipante'), id])
			redirect(action: "show", id: id)
		}
	}
}